import Vue            from 'vue'
import { apiRequest } from "../../assets/fetch";


const clueStatus = (item) => {
    return item.owner ? '已分配' : '待分配';
};

export default {
    namespaced: true,
    state     : {
        data: [],
        meta: {},
        all : [],
    },
    mutations : {
        saveData(state, data) {
            state.data = data.map((item) => {
                item.owner  = item.distributions.length ? item.distributions[ 0 ].name : '';
                item.status = clueStatus(item);
                return item;
            });
        },
        meta(state, meta) {
            state.meta = meta;
        },
        saveAll(state, data) {
            state.all = data;
        }
    },
    actions   : {
        async getDataForType({ commit }, data) {

            let res = await apiRequest('CLUE_INDEX', {
                data,
            });

            if (res.result) {
                commit('meta', res.data.meta);
                commit('saveData', res.data.data);
            }

            return res;
        },
        async distribution(_, { userId, id }) {
            return await apiRequest('DISTRIBUTION_CLUE', { data: { id } }, userId);
        }
    }
}
